package com.xinhe.web.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.AdoptedRange;
import com.xinhe.web.entity.AdoptedTree;
import com.xinhe.web.entity.QAdoptedTree;
import com.xinhe.web.reposity.AdoptedRangeRepository;
import com.xinhe.web.reposity.AdoptedTreeOrderRepository;
import com.xinhe.web.reposity.AdoptedTreeRepository;
import com.xinhe.web.util.Snowflake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import java.math.BigDecimal;

@Service

public class AdoptedTreeService {
    @Autowired
    AdoptedTreeRepository adoptedTreeRepository;
    @Autowired
    AdoptedRangeRepository adoptedRangeRepository;
    @Autowired
    AdoptedTreeOrderRepository adoptedTreeOrderRepository;
    //查询工厂
    private JPAQueryFactory queryFactory;
    @Autowired
    private Provider<EntityManager> entityManager;
    @PostConstruct
    public void init() {
        queryFactory = new JPAQueryFactory(entityManager);
    }

    public QueryResults<AdoptedTree> pageAdoptedTree(int pageNum, int pageSize) {
        QAdoptedTree qAdoptedTree = QAdoptedTree.adoptedTree;
        BooleanBuilder builder = new BooleanBuilder();
        return queryFactory.select(
                Projections.bean(
                        AdoptedTree.class,
                        qAdoptedTree.id,
                        qAdoptedTree.name,
                        qAdoptedTree.iconPath,
                        qAdoptedTree.adoptedYear,
                        qAdoptedTree.yunChaSeed,
                        qAdoptedTree.teaOilCard,
                        qAdoptedTree.consumerGoldRedEnvelope,
                        qAdoptedTree.price
                )
        ).from(qAdoptedTree)
                .where(builder).offset(pageNum * pageSize).limit(pageSize).fetchResults();
    }

    public Result<AdoptedTree> detailAdoptedTree(Long adoptedTreeId, Integer num) {
        AdoptedTree adoptedTreeView = adoptedTreeRepository.findById(adoptedTreeId).get();
        AdoptedRange rangeDaoByOne = null;
        Integer adoptedNumber = this.adoptedTreeOrderRepository.findByCompanySum();
        //Integer sumAdopted = this.valueConfigCache.getInteger("gift_cloud_tea_seed_welfare");
        Integer sumAdopted = 15000;//赠送云茶籽福利只限前15000颗
        System.out.println("现在已下单(已经支付的)认养数量为：" + adoptedNumber);
        if (adoptedNumber.intValue() < sumAdopted.intValue()) {
            AdoptedRange one = adoptedRangeRepository.findByOne();
            AdoptedRange two = adoptedRangeRepository.findByTwo();
            AdoptedRange three = adoptedRangeRepository.findByThree();
            if (one.getMinNum().intValue() <= num.intValue() && one.getMaxNum().intValue() >= num.intValue()) {
                adoptedTreeView.setYunChaSeed(one.getYunChaSeed());
            } else if (two.getMinNum().intValue() <= num.intValue() && two.getMaxNum().intValue() >= num.intValue()) {
                adoptedTreeView.setYunChaSeed(two.getYunChaSeed());
            } else {
                adoptedTreeView.setYunChaSeed(three.getYunChaSeed());
            }
        } else {
            adoptedTreeView.setYunChaSeed(BigDecimal.ZERO);
        }
        return Result.ok(adoptedTreeView);
    }

    public Result saveModel(Long id, String name, String iconPath, Integer adoptedYear, BigDecimal yunChaSeed, BigDecimal teaOilCard, BigDecimal consumerGoldRedEnvelope, BigDecimal price, String remark) {
        AdoptedTree adoptedTree = new AdoptedTree();
        adoptedTree.setId(Snowflake.generateId());
        adoptedTree.setName(name);
        adoptedTree.setIconPath(iconPath);
        adoptedTree.setAdoptedYear(adoptedYear);
        adoptedTree.setYunChaSeed(yunChaSeed);
        adoptedTree.setTeaOilCard(teaOilCard);
        adoptedTree.setConsumerGoldRedEnvelope(consumerGoldRedEnvelope);
        adoptedTreeRepository.save(adoptedTree);
        return Result.ok();
    }

    public Result<AdoptedTree> findById(Long adoptedTreeId) {
        return Result.ok(adoptedTreeRepository.findById(adoptedTreeId).get());
    }

    public Result delete(Long adoptedTreeId) {
        adoptedTreeRepository.deleteById(adoptedTreeId);
        return Result.ok();
    }

}
